package com.xzy.web.controller;

import com.xzy.custom.AjaxResult;
import com.xzy.custom.Page;
import com.xzy.custom.ResultMap;
import com.xzy.entities.SysRole;
import com.xzy.entities.SysUser;
import com.xzy.security.UserUtils;
import com.xzy.service.SysRoleService;
import com.xzy.service.SysUserService;
import com.xzy.web.form.SysUserForm;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * TODO
 *0
 * @author XiangZhongyi
 * ClassName: 用户管理
 * date: 2021/9/20 19:58
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private UserUtils userUtils;

    @RequestMapping("/userInformation")
    public String userInformation(){
        return "admin/user/userInformation/manage";
    }

    @RequestMapping("/findAllUserInformation")
    @ResponseBody
    public ResultMap<List<SysUser>> findAllUserInformation(Page page, @RequestParam("limit")int limit,@Param("name") String name){
        page.setRows(limit);
        page.setPage((page.getPage()-1)*limit);
        Integer total = sysUserService.findAllUserInformationCount(page,name);
        List<SysUser> list = sysUserService.findAllUserInformation(page,name);
        System.out.println(list);
        return new ResultMap<List<SysUser>>("",list,0,total);
    }

    @RequestMapping("/findNnmber")
    @ResponseBody
    public Object findNnmber(){
        String number = sysUserService.findNumber();
        return number;
    }

    @RequestMapping("/userInformationEdit")
    @ResponseBody
    public String userInformationEdit(){
        return "informationEdit";
    }

    @RequestMapping("/informationEdit")
    public String informationEdit(SysUserForm form, ModelMap map){
        SysUser model = new SysUser();
        Integer id = form.getId();
        System.out.println(id);
        if (id != null){
            model = sysUserService.findById(id);
        }
        map.put("model",model);
        map.put("userType",sysRoleService.findAllUserType1());
        return "admin/user/userInformation/edit";
    }

    @RequestMapping("/saveInformation")
    @ResponseBody
    public Object saveInformation(SysUserForm form,Integer rid){
        try {
            SysUser user = sysUserService.findByUserName(form.getUsername());
            if ("".equals(form.getUsername())){
                return new AjaxResult(false,"请输入用户名！");
            }else if ("".equals(form.getName())){
                return new AjaxResult(false,"请输入姓名！");
            }else if ("".equals(form.getUserPhone())){
                return new AjaxResult(false,"请输入电话号码！");
            }else if ("".equals(form.getUserAddress())){
                return new AjaxResult(false,"请输入家庭住址！");
            }else if ("".equals(form.getEmail())){
                return new AjaxResult(false,"请输入电子邮箱！");
            }else if ("".equals(form.getNumber())){
                return new AjaxResult(false,"请输入管理员编号！");
            }else if ("".equals(form.getIdCard())){
                return new AjaxResult(false,"请输入身份证号！");
            }else if ("".equals(form.getUserSex())){
                return new AjaxResult(false,"请选择性别！");
            }else if (rid == null){
                return new AjaxResult(false,"请选择用户权限！");
            }else{
                BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
                form.setPassword(encoder.encode("123456"));
                if (form.getId() != null){
                    sysUserService.updateById(form,rid);
                }else{
                    if (user != null){
                        return new AjaxResult(false,"用户名已存在！");
                    }else{
                        sysUserService.insertUserRole(form,rid);
                    }
                }
            }
            return new AjaxResult("数据保存成功！");
        }catch (Exception e){
            return new AjaxResult(false,"数据保存失败！");
        }
    }

    @RequestMapping("/delUserInformation")
    @ResponseBody
    public Object delUserInformation(String ids){
        try {
            String[] spilt = ids.split(",");
            for (int i = 0; i < spilt.length;i++){
                sysUserService.delUserInformation(Integer.parseInt(spilt[i]));
            }
            return new AjaxResult("删除成功");
        }catch (Exception e){
            return new AjaxResult(false,"数据删除失败！");
        }

    }

    @RequestMapping("/info")
    public String info(ModelMap map){
        SysUser user = sysUserService.findById(userUtils.getUser().getId());
        List<SysRole> role = sysRoleService.findAllUserType1();
        map.put("model",user);
        map.put("role",role);
        return "admin/user/info/manage";
    }

    @RequestMapping("/password")
    public String password(){
        return "admin/user/info/changePwd";
    }

    @RequestMapping("/changePwd")
    @ResponseBody
    public Object changePwd(String oldPwd,String newPwd,String affirmPwd){
        try {
            SysUser user = sysUserService.findById(userUtils.getUser().getId());
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String newP = encoder.encode(newPwd);
            if("".equals(oldPwd)){
                return new AjaxResult(false,"请输入原密码！");
            }else if("".equals(newPwd)){
                return new AjaxResult(false,"请输入新密码！");
            }else if("".equals(affirmPwd)){
                return new AjaxResult(false,"请确认新密码！");
            }else if(!newPwd.equals(affirmPwd)){
                return new AjaxResult(false,"两次密码输入不一致！");
            }else if(!encoder.matches(oldPwd,user.getPassword())){
                return new AjaxResult(false,"原密码输入错误！");
            }else{
                sysUserService.updatePwdById(newP,user.getId());
                return new AjaxResult(false,"修改成功，请刷新网页重新登录！");
            }
        }catch (Exception e){
            return new AjaxResult(false,"修改失败！");
        }
    }
}
